#include "test1.h"

static JSBool
js__c001(JSContext *cx, JSObject *__this, uintN argc, jsval *argv, jsval *rval) {
	char *filename;
	JS_ConvertArguments(cx, argc, argv, "s", &filename);

	FILE *f = fopen(filename, "r");
	char *buf = (char *)malloc(1000000);
	int len = fread(buf, 1, 1000000, f);
	buf[len] = 0;
	

	jsval v;
	v = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, buf));	
	JS_SetProperty(cx, __this, "data", &v);
	return JS_TRUE;
};


//BEGIN_EXTERN_C


int init(JSContext *cx, JSRuntime *rt) {
	printf("hello from test1.c!\n");
	printf("cx=%p, rt=%p\n", cx, rt);

	//JS_EndRequest(cx);
	
	//JS_ReportError(cx, "fuck!");
	
	//fatal("zhopa!");
	
	
	
	//JSString *str = JS_NewStringCopyZ(cx, "zhopa");
	
	
	//return;
	
	//jsval r;
	//jsval args[1];
	//args[0] = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, "zhopa"));
	
	
	//JS_CallFunctionName(cx, JS_GetGlobalObject(cx), "alert", 1, args, &r);


	//JS_SetGlobalObject(cx, obj);

	

	//jsval v;
	//if (!JS_GetProperty(cx, JS_GetGlobalObject(cx), "JSON", &v)) {
		//JS_ReportError(cx, "C object is not defined");
	//	return;
	//}
	/*
	JSObject *obj;
	JS_ValueToObject(cx, v, &obj);
	*/

	//JS_DefineFunction(cx, JS_GetGlobalObject(cx), "js__c001", js__c001, 1, 0);
	return 0;
}

//END_EXTERN_C


//int main(){
//	printf("test1.c: main()!\n");
//}



